package com.bravo.other.time;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 测试写的简易时间轮
 *
 *
 */
public class TimingWheelTest {

    public static void main(String[] args) throws InterruptedException {
        testBasicFunctionality();
        testTaskExecution();
        testTaskNotAddedWhenDelayTooLarge();
        System.out.println("所有测试通过!");
    }

    /**
     * 测试时间轮的基本功能
     */
    public static void testBasicFunctionality() {
        System.out.println("开始测试基本功能...");
        TimingWheel timingWheel = new TimingWheel(1, 8); // tickDuration=1, wheelSize=8
        
        // 验证基本属性
        assert timingWheel.getTickDuration() == 1;
        assert timingWheel.getWheelSize() == 8;
        assert timingWheel.getCurrentTime() == 0;
        System.out.println("基本属性测试通过");
    }

    /**
     * 测试任务执行
     */
    public static void testTaskExecution() throws InterruptedException {
        System.out.println("开始测试任务执行...");
        TimingWheel timingWheel = new TimingWheel(100, 8); // tickDuration=100ms, wheelSize=8
        
        AtomicInteger executedCount = new AtomicInteger(0);
        CountDownLatch latch = new CountDownLatch(1);
        
        // 创建一个延迟200ms的任务
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                executedCount.incrementAndGet();
                System.out.println("任务执行时间: " + System.currentTimeMillis());
                latch.countDown();
            }
            
            @Override
            public long getDelay() {
                return 200; // 200ms后执行
            }
        };
        
        // 添加任务到时间轮
        boolean added = timingWheel.addTask(task);
        assert added : "任务应该被成功添加";
        System.out.println("任务已添加到时间轮");
        
        // 推进时间轮到任务应该执行的时间点
        long startTime = System.currentTimeMillis();
        timingWheel.advanceClock(startTime +200);
        
        // 等待任务执行完成
        boolean completed = latch.await(1, TimeUnit.SECONDS);
        assert completed : "任务应该在指定时间内执行完成";
        assert executedCount.get() == 1 : "任务应该被执行一次";
        
        System.out.println("任务执行测试通过，耗时: " + (System.currentTimeMillis() - startTime) + "ms");
    }

    /**
     * 测试延迟过大的任务不会被添加
     */
    public static void testTaskNotAddedWhenDelayTooLarge() {
        System.out.println("开始测试延迟过大的任务...");
        TimingWheel timingWheel = new TimingWheel(10, 8); // tickDuration=10, wheelSize=8
        // 时间轮总容量为 10 * 8 = 80
         
        TimerTask task = new TimerTask() {
            @Override
            public void run() {
                // 任务实现
                System.out.println("任务执行时间: " + System.currentTimeMillis());
            }
            
            @Override
            public long getDelay() {
                return 100; // 超过时间轮容量(80)的延迟
            }
        };
        
        // 尝试添加任务
        boolean added = timingWheel.addTask(task);
        assert !added : "延迟超过时间轮容量的任务不应该被添加";
        System.out.println("延迟过大任务测试通过");
    }
}
